<?php

declare(strict_types=1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Menu extends Model
{
    protected $schema = [
        'id' => 'bigint',
        'menu_name' => 'varchar',
        'parent_id' => 'bigint',
        'order_num' => 'int',
        'path' => 'varchar',
        'component' => 'varchar',
        'query' => 'varchar',
        'route_name' => 'varchar',
        'is_frame' => 'char',
        'is_cache' => 'char',
        'menu_type' => 'char',
        'visible' => 'char',
        'status' => 'char',
        'perms' => 'varchar',
        'icon' => 'varchar',
        'create_by' => 'varchar',
        'create_time' => 'datetime',
        'update_by' => 'varchar',
        'update_time' => 'datetime',
        'remark' => 'varchar'
    ];
    //字段映射
    // protected $mapping = [
    //     'menuName' => 'menu_name',
    //     'pid' => 'parent_id',
    //     'orderNum' => 'order_num',
    //     'path' => 'path',
    //     'component' => 'component',
    //     'routeName' => 'route_name',
    //     'isFrame' => 'is_frame',
    //     'isCache' => 'is_cache',
    //     'menuType' => 'menu_type',
    //     'icon' => 'icon',
    //     'perms' => 'perms',
    //     'status' => 'status',
    //     'createTime' => 'create_time',
    //     'updateTime' => 'update_time',
    //     'createBy' => 'create_by',
    //     'updateBy' => 'update_by'
    // ];
        protected $mapping = [
        'menu_name' => 'menuName',
        'parent_id' => 'pid',
        'order_num' => 'orderNum',
        'route_name' => 'routeName',
        'is_frame' => 'isFrame',
        'is_cache' => 'isCache',
        'menu_type' => 'menuType',
        'create_time' => 'createTime',
        'update_time' => 'updateTime',
        'create_by' => 'createBy',
        'update_by' => 'updateBy'
    ];
    //关联角色
    public function role()
    {
        return $this->belongsToMany(Role::class, 'role_menu', 'role_id', 'menu_id');
    }

    // public function getMappedField($field)
    // {
    //     return isset($this->mapping) ? $this->mapping[$field] : $field;
    // }
    public function getMappedField($field)
    {
        $mapping =  array_search($field, $this->mapping);
        return $mapping ?? $field;
    }
}
